//logs.js
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
Page({
  data: {
    headIndex:0,
    pageIndex: 1,
    pageCount: 10,
    dataList: [],
    dataDict:{},
    requestUrl:"",
    withdrawMoney:"",

  },
  onLoad: function () {
    this.getCanUserMoney();
    this.getListData(app.mineGiveDaShangUrl);
  },
  orginClcik(){
    this.setData({
      headIndex:0
    });
    this.data.pageIndex = 1;
    this.data.dataList = [];
    this.getListData(app.mineGiveDaShangUrl);

  },
  orginGetClcik(){
    this.setData({
      headIndex:1
    });
    this.data.pageIndex = 1;
    this.data.dataList = [];
    this.getListData(app.mineGetDaShangUrl);

  },
  allWithDraw(){
    this.setData({
      withdrawMoney:this.data.dataDict.withdrawAmount+""
    });
  },
  getCanUserMoney(){
    var _this = this;
    var map = {};
    map.userId = app.USER_ID();
    map.type = "5";
    api.getRequestData(app.mineDaShangDetailUrl, map, false, "POST").then(res => {
      if (res.data.errorCode == 0) {
        console.log("=====",JSON.stringify(res.data.model));
        _this.setData({
          dataDict:res.data.model
        });
      } else {
        app.alert(res.data.errorMsg);
      }
    });
  },
  getListData(url) {
    this.data.requestUrl = url;
    var _this = this;
    var map = {};
    map.userId = app.USER_ID();
    map.pageCount = this.data.pageCount;
    map.pageIndex = this.data.pageIndex;
    map.type = "5";
    api.getRequestData(url, map, true, "POST").then(res => {
      if (res.data.errorCode == 0) {
        var list = res.data.model.list;
        console.log(list[0]);
        if (list.length == 0) {
          app.alert("没有更多数据了～");
        }
        if (_this.data.pageIndex == 1) {
          _this.data.dataList = list;
          _this.setData({
            dataList: _this.data.dataList
          });
        } else {
          // 加载更多
          _this.setData({
            dataList: _this.data.dataList.concat(list)
          });
        }
        
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    });
  },
  withdrawInput(e){
    var value = e.detail.value;
    this.data.withdrawMoney = value;
  },
  onPullDownRefresh: function() {
    this.data.pageIndex = 1;
    this.getListData(this.data.requestUrl);

  },
  onReachBottom: function() {
    this.data.pageIndex += 1;
    this.getListData(this.data.requestUrl);
  },
  goToWithdrawList(){
    wx.navigateTo({
      url: '/pages/mine/withdrawList/withdrawList',
    })
  },
  withdrawNowClick(){
    if (this.data.withdrawMoney.length == 0){
      app.alert("请输入提现金额~");
      return;
    }
    var money = parseFloat(this.data.withdrawMoney);
    if (!app.isNumber(money)){
      app.alert("请输入正确提现金额~");
      return;
    }
    if (money < 100){
      app.alert("提现金额必须大于等于100");
      return;
    }
    var _this = this;
    var map = {};
    map.userId = app.USER_ID();
    map.amount = this.data.withdrawMoney;
    map.type = "5";
    map.appId = app.APP_ID;
    api.getRequestData(app.withdrawUrl, map, true, "POST").then(res => {
      if (res.data.errorCode == 0) {
        wx.showToast({
          title: "提现申请成功~",
          icon: 'success',
          mask: true,
          duration: 1500
        });
        setTimeout(() => {
          _this.getCanUserMoney();
        }, 1400);
      } else {
        app.alert(res.data.errorMsg);
      }
    });
  },
  goToPostDetail(e) {
    var index = e.currentTarget.dataset.key;
    var dict = this.data.dataList[index];
    var userId = dict.userId;
    var id = dict.postId;
    var path = '/pages/index/detail/detail?USER_ID=' + userId + '&POST_ID=' + id;
    wx.navigateTo({
      url: path,
    })
  },
})
